/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.action;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.loader.LoadResultListStoreBinding;
import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;
import com.sencha.gxt.data.shared.loader.PagingLoader;
import com.sencha.gxt.widget.core.client.toolbar.PagingToolBar;


/**
 * A paging loader for data in memory
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 * @param <B>
 */
public class DkBaseMemoryPagingLoader<B extends Serializable> extends PagingLoader<PagingLoadConfig, PagingLoadResult<B>>
{

	private final PagingToolBar pagingToolbar;
	private final DkBaseMemoryPagingLoaderProxy<B> proxy;

	/**
	 * Constructor
	 * 
	 * @param store
	 *           the store to bind this
	 * @param pageSize
	 *           the paging size
	 * @param proxy
	 *           the proxy (a new {@link DkBaseMemoryPagingLoaderProxy}() is enough)
	 */
	public DkBaseMemoryPagingLoader(final ListStore<B> store, final int pageSize, final DkBaseMemoryPagingLoaderProxy<B> proxy)
	{
		super(proxy, proxy.getReader());
		setRemoteSort(false);
		pagingToolbar = new PagingToolBar(pageSize);
		pagingToolbar.bind(this);
		this.proxy = proxy;
		addLoadHandler(new LoadResultListStoreBinding<PagingLoadConfig, B, PagingLoadResult<B>>(store));
	}

	/**
	 * Set the data in the memory PROXY and reload this loader
	 * 
	 * @param data
	 *           the data to set
	 */
	public void setData(final List<B> data)
	{
		proxy.setData(data);
		load();
	}

	/**
	 * Clear the data in the memory PROXY and reload this loader
	 */
	public void clearData()
	{
		setData(new ArrayList<B>());
	}

	/**
	 * @return the pagingToolbar
	 */
	public PagingToolBar getPagingToolbar()
	{
		return pagingToolbar;
	}
}
